/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.service;

import br.com.foxline.model.City;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
@LocalBean
public class CityService extends GenericService<City> {

    public CityService() {
        super(City.class);
    }

    public List<City> autocomplete(String name) {
        List<City> lista = new ArrayList<City>();
        String sql = "Select c from City c where lower(c.name) like lower(:name)";
        Query query = getEntityManager().createQuery(sql).setMaxResults(50);

        if (name != null) {
            query.setParameter("name", "" + name + "%");
        }

        lista = query.getResultList();
        return lista;
    }
 
    public List<City> findCity(City city) {
        String sql = "select c from City c ";

        if ((!city.getName().equals("")) && (city.getName() != null)) {
            sql += "  WHERE lower(c.name) like lower(:name)  ";
        }
        Query query = getEntityManager().createQuery(sql);

        if ((!city.getName().equals("")) && (city.getName() != null)) {
            query.setParameter("name", "%" + city.getName() + "%");
        }
        return query.getResultList();

    }
}
